package com.monkeyk.sos.web.controller.portal.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.monkeyk.sos.domain.dto.ApplicationDetailsDto;
import com.monkeyk.sos.domain.model.ResourceDetail;
import com.monkeyk.sos.domain.shared.GuidGenerator;
import com.monkeyk.sos.domain.shared.security.WdcyUserDetails;
import com.monkeyk.sos.service.ApplicationService;
import com.monkeyk.sos.service.CooperService;
import com.monkeyk.sos.service.OauthService;
import com.monkeyk.sos.service.UserService;
import com.monkeyk.sos.web.controller.BaseController;

/**
 * 
* @ClassName: WebController 
* @Description: 网站接入控制层
* @author cygaod 
* @date 2015年12月8日 下午9:04:31 
* @version 1.0
 */

@Controller
@RequestMapping("/portal/web")
public class WebController extends BaseController {
	private static final Logger	Log	= Logger.getLogger(WebController.class);
	@Autowired
	private UserService			userService;

	@Autowired
	private OauthService		oauthService;

	@Autowired
	private ApplicationService	applicationService;
	
	@Autowired
	private CooperService	cooperService;
	
	private static String property;
	private static String content;

	/**
	* @Title: webDetail 
	* @Description: 应用申请初始页 
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */
	@RequestMapping("/apply")
	public String apply(Model model) {
		Log.info("webApply ：" + "web/webDetail");
		property="wb:webmaster";
		content=GuidGenerator.generate();
		model.addAttribute("property",property );
		model.addAttribute("content", content);
		return "portal/web/applyCheck";
	}

	/**
	*
	* @Title: saveAndCheckApp 
	* @Description: 网站应用申请
	* @param @param request
	* @param @param response    设定文件 
	* @return void    返回类型 
	* @throws
	 */
	@ResponseBody
	@SuppressWarnings("unused")
	@RequestMapping(value = "/saveAndCheckApp", method = RequestMethod.POST)
	public void saveAndCheckApp(ApplicationDetailsDto appDetails,HttpServletResponse response) {
		Log.info("web ：" + "web/saveAndCheckApp");
		int cooperId = ((WdcyUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser().getId();
		Map<String, Boolean> resultMap = new HashMap<String, Boolean>();
		String appId = applicationService.registerApplicationDetails(appDetails,cooperId);
		resultMap.put("status", true);
		writeJSON(response, resultMap);
		/*if (WebUtils.checkWebOwner("http://localhost:8090/spring-oauth-client/",property,content)) {
			String appId = applicationService.registerApplicationDetails(appDetails,cooperId);
			resultMap.put("status", true);
			writeJSON(response, resultMap);
		} else {
			resultMap.put("status", false);
			writeJSON(response, resultMap);
		};*/
	}

	/**
	* @Title: webAppDetail 
	* @Description: 应用详情模板
	* @param @param appId
	* @param @param model
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */
	@RequestMapping("/webAppDetail/{appId}")
	public String webTemplet(@PathVariable("appId") String appId, Model model) {
		Log.info("应用详情模板 (webTemplet ：appId:" + appId+")");
		ApplicationDetailsDto appDetailsDto = applicationService.getApplicationDetailsDto(appId);
		model.addAttribute("appDetailsDto", appDetailsDto);
		return "portal/web/templet";
	}
	
	
	/**
	* @Title: webAppDetailD 
	* @Description: 应用详情首页 
	* @param @param appId
	* @param @param model
	* @param @param response
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */
	@RequestMapping("/info/webAppDetail/{appId}/{page}")
	public String webAppDetail(@PathVariable("appId") String appId, @PathVariable("page") String page,Model model) {
		Log.info("应用详情首页 (webAppDetail ：appId:" + appId+")");
		ApplicationDetailsDto appDetailsDto = applicationService.getApplicationDetailsDto(appId);
		if(page.equals("privilege")){
			List<ResourceDetail> resourceList = cooperService.getResource();
			model.addAttribute("resourceList", resourceList);
		};
		model.addAttribute("appDetailsDto", appDetailsDto);
		return "portal/web/webAppManage/"+page;
	}
	
	/**
	 * 
	*
	* @Title: updateWebAppDetail 
	* @Description: 更新应用 基本信息
	* @param @param appDetails
	* @param @param model
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */
	@RequestMapping(value="/info/updateBasicAppDetail",method = RequestMethod.POST)
	public String updateBasicAppDetail(ApplicationDetailsDto appDetails, Model model) {
		String appId = appDetails.getAppId();
		Log.info("更新应用 基本信息(updateWebAppDetail ：appId:" + appId+")");
		applicationService.updateApplicationDetails(appDetails,appId);
		ApplicationDetailsDto appDetailsDto = applicationService.getApplicationDetailsDto(appId);
		model.addAttribute("appDetailsDto", appDetailsDto);
		return "portal/web/webAppManage/basic";
	}
	
	/**
	* @Title: updateAdvancedAppDetail 
	* @Description: 更新应用高级信息 
	* @param @param appDetails
	* @param @param model
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */
	@RequestMapping(value="/info/updateAdvancedAppDetail",method = RequestMethod.POST)
	public String updateAdvancedAppDetail(ApplicationDetailsDto appDetails, Model model) {
		String appId = appDetails.getAppId();
		Log.info("更新应用高级信息(updateAdvancedAppDetail ：appId:" + appId+")");
		applicationService.updateAdvancedAppDetail(appDetails,appDetails.getClientId());
		ApplicationDetailsDto appDetailsDto = applicationService.getApplicationDetailsDto(appId);
		model.addAttribute("appDetailsDto", appDetailsDto);
		return "portal/web/webAppManage/advanced";
	}
	/**
	* @Title: updateAppStatus 
	* @Description: 更新应用状态信息 
	* @param @param appId
	* @param @param status
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */
	@RequestMapping(value="/info/updateAppStatus")
	public String updateAppStatus(String appId, int status,Model model) {
		Log.info("更新应用高级信息(updateAdvancedAppDetail ：appId:" + appId+")");
		applicationService.updateAppStatus(appId, status);
		ApplicationDetailsDto appDetailsDto = applicationService.getApplicationDetailsDto(appId);
		model.addAttribute("appDetailsDto", appDetailsDto);
		return "portal/web/webAppManage/basic";
	}
	
	
	
	/**
	* @Title: resetClientSecret 
	* @Description: 重置clientSecret
	* @param @param appDetails
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */
	@RequestMapping(value="/resetClientSecret",method = RequestMethod.POST)
	public void resetClientSecret(HttpServletRequest request,HttpServletResponse response) {
		String clientSecret = applicationService.resetClientSecret(request.getParameter("clientId"));
		Map<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("clientSecret", clientSecret);
		writeJSON(response, resultMap);
	}
	
	/**
	* @Title: deletApp 
	* @Description: 删除应用 
	* @param @param request
	* @param @param response    设定文件 
	* @return void    返回类型 
	* @throws
	 */
	@RequestMapping(value="/deletApp",method = RequestMethod.POST)
	public void deletApp(String appId,HttpServletResponse response) {
		boolean status = applicationService.deletApp(appId);
		Map<String, Boolean> resultMap = new HashMap<String, Boolean>();
		resultMap.put("status", status);
		writeJSON(response, resultMap);
	}
	
	
	
	/**
	* @Title: checkWebName 
	* @Description: 校验web应用应用名的唯一性 
	* @param @param webName
	* @param @param appId
	* @param @param webNameNow
	* @param @param response
	* @param @throws IOException    设定文件 
	* @return void    返回类型 
	* @throws
	 */
	@RequestMapping("/checkWebName")
	public void checkWebName(String webName,String appId,String webNameNow,HttpServletResponse response) throws IOException {
		response.reset();
        boolean status = applicationService.checkWebName(webName);
        if(status){
        	response.getWriter().print(status);
        }else{
        	if(appId == ""){
        		response.getWriter().print(status);
            }else{
            	if(webNameNow.equals(webName)){
            		response.getWriter().print(true);
                }else {
    				response.getWriter().print(false);
    			} 
            }
        }
	}
	
}
